(ns datamodel
(:use [net.cgrand.xml :as en-xml]
[clojure.data.zip.xml :only (attr text xml->)])
  (:require [clojure.xml :as xml]
            [clojure.zip :as zip]
            [clojure.contrib.zip-filter.xml :as zf]
            [apicalls]
            [clojure.java.io]
            [clojure.contrib.str-utils] ))
 

;structs

;(defstruct event :id :url :event-name :duration :venue :allday :recur-instances :performers :image :categories)
(defstruct event :event-name :performers :start-time :end-time)

(defstruct venue :id :name :location)
(defstruct location :lat :long :name)
(defstruct image :url :width :height :thumb)
(defstruct category :id)
(defstruct artist-lastfm :name :mbid :url :summary)
(defstruct artist-musicbrainz :gender :country :life-span)
(defstruct tag-list :tag :name :url)

(def events-url "http://api.eventful.com/rest/events/search?app_key=4H4Vff4PdrTGp3vV&keywords=music&location=Belgrade&date=Future")

;(defn get-data-from-feed [feed]
 ; (
  ;  let[xml (events-url)
   ;    zipp (zip/xml-zip xml)
    ;   elements(-> zipp zip/down zip/children)
     ;  events (filter #(= :event (:tag %)) elements)]
    ;(map create-map events)
   ; ))



;processor functions


  

(defn eventfultoToEvent[evffeed]
  "Events from eventful transfered to struct"
  
  )

;aggregator functions for content enricher
;aggregate(old_message,new_message)
;old message is html template
;new is html template with event and artist data
 ;parsing functions
 (defn musicBrainzToArtist[xz]
  "Artists from musicBrainz transfered to struct from zipper tree made of feed output"
  (map (juxt 
        ;#(zf/xml1-> % :name text)  
        #(zf/xml1-> % :gender text) 
         #(zf/xml1-> % :country text)
         #(zf/xml1-> % :life-span :begin text)
         )
     (zf/xml-> xz :artist-list :artist))
  )

(defn lastFmToArtist[xz]
  "Artists from last.fm transfered to struct from zipper tree made of feed output"
  (map (juxt 
        #(zf/xml1-> % :name text)  
        #(zf/xml1-> % :mbid text) 
         #(zf/xml1-> % :url text)
         #(zf/xml1-> % :bio :summary text)
         )
     (zf/xml-> xz :artist))
  )

(defstruct event-map  :title  :event-data)

(defn events-for-mashup []
  (let [title "Events mashup" event-data (vector (create-map-of-events))] 
    (apply struct event-map title event-data)))
 
(defn get-events
  [xz] 
  (map (juxt 
        #(zf/xml1-> % :title text) 
        #(zf/xml1-> % :performers :performer :name text) 
        #(zf/xml1-> % :start_time text) 
         #(zf/xml1-> % :end_time text)
         )
     (zf/xml-> xz :events :event)))

 (defn create-map-of-events []
  (map #(apply struct event %) (get-events (zip/xml-zip (xml/parse "http://api.eventful.com/rest/events/search?app_key=4H4Vff4PdrTGp3vV&keywords=music&location=Belgrade&date=Future")))))

 (defn create-map-of-artists-lastfm  []
  (map #(apply struct artist-lastfm %) (lastFmToArtist (zip/xml-zip (xml/parse "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Cher&api_key=b25b959554ed76058ac220b7b2e0a026")))))
 
 (defn create-map-of-artists-musicbrainz  []
  
   (map #(apply struct artist-musicbrainz %) (musicBrainzToArtist (zip/xml-zip (xml/parse "http://www.musicbrainz.org/ws/2/artist/?query=artist:cher")))))
 
(def events (xml/parse "http://api.eventful.com/rest/events/search?app_key=4H4Vff4PdrTGp3vV&keywords=music&location=Belgrade&date=Future"))

 (def zipped (zip/xml-zip events))

;_exchange.getOut().setBody(createEarthquake(title.substring(7), date, title.substring(2,5), latitude, longitude, depth, area))
